package com.andatsoft.laisim.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.dialog.MessageBox;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.ToastMessage;

public class EditLyricsActivity extends BaseActivity {

	private TextView mBtnSave;
	private TextView mBtnCancel;
	private EditText mEtContent;
	private int mSongId;
	private boolean mIsMessageBoxShowing;
	private MessageBox mMessageBox;
	private boolean mIsMessageBoxReady = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_edit_lyrics);

		mBtnSave = (TextView) findViewById(R.id.tv_edit_lyrics_save);
		setBackgroundColorForView(mBtnSave);
		mBtnSave.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());
		mBtnCancel = (TextView) findViewById(R.id.tv_edit_lyrics_cancel);
		setBackgroundColorForView(mBtnCancel);
		mBtnCancel.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());
		mEtContent = (EditText) findViewById(R.id.et_lyrics);
		mEtContent.setBackgroundColor(getSelectedTheme()
				.getDialogTextColor());
		mEtContent.setTextColor(getSelectedTheme()
				.getDialogBackgroundColor() | 0xff000000);

		mSongId = getIntent().getExtras().getInt(Common.SONG_ID_INTENT, -1);
		if (mSongId == -1) {
			mEtContent.setEnabled(false);
			mBtnSave.setEnabled(false);
			mBtnCancel.setEnabled(false);
			Toast.makeText(getApplicationContext(),
					getString(R.string.edit_toast_message_invalid_song_id),
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (mEtContent == null) {
			return;
		}
		String ly = getIntent().getExtras()
				.getString(Common.SONG_LYRICS_INTENT);
		mEtContent.setText(ly);

		mBtnCancel.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					mMessageBox = new MessageBox(getApplicationContext(), true);
					mMessageBox
							.setMessageContent(getString(R.string.edit_lyrics_message_cancel_lyrics_saving));
					mMessageBox
							.setOkButtonOnTouchListener(new OnTouchListener() {

								@Override
								public boolean onTouch(View v, MotionEvent event) {
									if (!mIsMessageBoxReady)
										return true;
									switch (event.getAction()) {
									case MotionEvent.ACTION_DOWN:
										setAlpha(v, 0.5f);
										return true;

									case MotionEvent.ACTION_UP:
										setAlpha(v, 1.0f);
										mIsMessageBoxReady = false;
										EditLyricsActivity.this
												.setResult(
														Common.RESULT_CODE_FOR_LYRICS_FRAGMENT_FROM_LYRICS_EDITOR,
														null);
										EditLyricsActivity.this.finish();
										break;
									}
									return false;
								}
							});
					mMessageBox
							.setCancelButtonOnTouchListener(new OnTouchListener() {

								@Override
								public boolean onTouch(View v, MotionEvent event) {
									if (!mIsMessageBoxReady)
										return true;
									switch (event.getAction()) {
									case MotionEvent.ACTION_DOWN:
										setAlpha(v, 0.5f);
										return true;

									case MotionEvent.ACTION_UP:
										setAlpha(v, 1.0f);
										mIsMessageBoxReady = false;
										onBackPressed();
										break;
									}
									return false;
								}
							});

					LinearLayout.LayoutParams pr = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT,
							LinearLayout.LayoutParams.MATCH_PARENT);
					addContentView(mMessageBox, pr);
					Animation animation = AnimationUtils.loadAnimation(
							getBaseContext(),
							R.anim.message_box_slide_top_to_bottom_fade_in);
					animation.setStartOffset(0);
					mMessageBox.getContent().startAnimation(animation);
					animation = AnimationUtils.loadAnimation(getBaseContext(),
							R.anim.message_box_bg_fade_in_fast);
					animation.setFillAfter(true);
					mMessageBox.getBackgroundLayout().startAnimation(animation);
					mIsMessageBoxShowing = true;
					return true;
				}
				return false;
			}
		});
		mBtnSave.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					DataBaseAccess db = DataBaseAccess
							.getInstance(getApplicationContext());
					String lyrics = mEtContent.getText().toString();
					db.updateLyrics(mSongId, lyrics);
					EditLyricsActivity.this
							.setResult(
									Common.RESULT_CODE_FOR_LYRICS_FRAGMENT_FROM_LYRICS_EDITOR,
									null);
					new ToastMessage(getApplicationContext())
							.showShortMessage(getString(R.string.toast_saved));
					EditLyricsActivity.this.finish();
					return true;
				}
				return false;
			}
		});
	}

	@Override
	public void onBackPressed() {
		if (mIsMessageBoxShowing) {
			Animation animation = AnimationUtils.loadAnimation(
					getBaseContext(),
					R.anim.message_box_slide_bottom_to_top_fade_out);
			animation.setStartOffset(0);
			animation.setFillAfter(true);
			mMessageBox.getContent().startAnimation(animation);
			mMessageBox.getContent().setVisibility(View.INVISIBLE);
			animation = AnimationUtils.loadAnimation(getBaseContext(),
					R.anim.message_box_bg_fade_out_fast);
			animation.setStartOffset(0);
			animation.setFillAfter(true);
			mMessageBox.getBackgroundLayout().startAnimation(animation);
			mMessageBox.getBackgroundLayout().setClickable(false);
			animation.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {

				}

				@Override
				public void onAnimationRepeat(Animation animation) {

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					((ViewManager) mMessageBox.getParent())
							.removeView(mMessageBox);
					mMessageBox = null;
					mIsMessageBoxShowing = false;
					mIsMessageBoxReady = true;
				}
			});
		} else {
			super.onBackPressed();
		}
	}
}
